<?php

/**
 * This model class represents the business logic associated with a "setting" 
 * model.  While its easy to say that models are generally derived from 
 *
 */
class Model_Setting
{
    /** Model_Table_Setting */
    protected $_table;

    /*mode */
    const HEAT=0;
    const COOL=1;    
    /* scale */
    const FAHRENHEIT=0;
    const CELSIUS=1;


    /**
     * Retrieve table object
     * 
     * @return Model_Reading_Table
     */
    public function getTable()
    {
        if (null === $this->_table) {
            // since the dbTable is not a library item but an application item,
            // we must require it to use it
            require_once APPLICATION_PATH . '/models/DbTable/Setting.php';
            $this->_table = new Model_DbTable_Setting;
        }
        return $this->_table;
    }

    /**
     * Save a new setting
     * 
     * @param  array $data 
     * @return int|string
     */
    public function save(array $data)
    {
        $table  = $this->getTable();
        $fields = $table->info(Zend_Db_Table_Abstract::COLS);
        foreach ($data as $field => $value) {
            if (!in_array($field, $fields)) {
                unset($data[$field]);
            }
        }
        return $table->insert($data);
    }

    /**
     * Fetch all entries
     * 
     * @return Zend_Db_Table_Rowset_Abstract
     */
    public function fetchEntries()
    {
        // we are gonna return just an array of the data since
        // we are abstracting the datasource from the application,
        // at current, only our model will be aware of how to manipulate
        // the data source (dbTable).
        // This ALSO means that if you pass this model
        return $this->getTable()->fetchAll('1')->toArray();    
    }

    /**
     * Fetch an individual entry
     * 
     * @param  int|string $id 
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntry($id)
    {
        $table = $this->getTable();
        $select = $table->select()->where('id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $rs =  $table->fetchRow($select)->toArray();
        if (count($rs) > 0) { 
          return $rs->toArray();
        } else {
          return array();
        }
    }

    public function isHeat() 
    {
      $table = $this->getTable();
      $select = $table->select()->where('name = "mode"');     
      $rs =  $table->fetchRow($select)->toArray();
      if ($rs["value"] == Model_Setting::HEAT) {
        return true;
      }
      return false;
    }
    
    public function isCooling()
    {
      return !($this->isHeating());
    }
    

    public function isFahrenheit() 
    {
      $table = $this->getTable();
      $select = $table->select()->where('name = "scale"');     
      $rs =  $table->fetchRow($select)->toArray();
      if ($rs["value"] == Model_Setting::FAHRENHEIT) {
        return true;
      }
      return false;
    }
    
    
    public function isCelsius() 
    {
      return !($this->isFahrenheit());
    }


    public function setMode($value) 
    {
      $data['value'] = ($value == "cool") ? Model_Setting::COOL : Model_Setting::HEAT;
      $where = 'name = "mode"';
      $this->getTable()->update($data, $where);
    }
    

    public function setScale($value) 
    {
      $data['value'] = ($value == "C") ? Model_Setting::CELSIUS : Model_Setting::FAHRENHEIT;
      $where = 'name = "scale"';
      $this->getTable()->update($data, $where);
    }
}